package com.zyfu.demo;

import org.apache.kafka.clients.producer.ProducerRecord;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * <b>请输入名称</b>
 * <pre>
 * 描述<br/>
 * 作用：；<br/>
 * 限制：；<br/>
 * </pre>
 *
 * @author 张永福
 * @since  2024/3/14 10:23
 */
public class CustomProducer {

    private KafkaTemplate<String, String> kafkaTemplate;

    public void sendMsg(String topic, String data){
        try {
            CompletableFuture<SendResult<String, String>> send = kafkaTemplate.send(topic, data);
            SendResult<String, String> stringStringSendResult = send.get();
            ProducerRecord<String, String> producerRecord = stringStringSendResult.getProducerRecord();
            System.out.println("开始发送------------------------------------------------------------------------");
            System.out.println("发送消息：" + producerRecord.toString());
            System.out.println("发送完毕------------------------------------------------------------------------");
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }
    }

    public void setKafkaTemplate(KafkaTemplate<String, String> kafkaTemplate) {
        this.kafkaTemplate = kafkaTemplate;
    }
}
